﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using Facade;
using System.Collections;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;

namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class BlacklistUsers : SOSecurityPage, IntTest
    {
        private int pageNo = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
          
        }






        public void Source(string sn, int page)
        {
            try
            {
                string uName = string.Empty;

                if (string.IsNullOrEmpty(sn))
                {
                    uName = "%" + txtUserName.Text + "%";

                    if (page == 1)
                    {
                        UCGrid1.SetText("1");
                        Session["SUsr"] = uName;
                    }

                }
                else
                {
                    uName = sn;
                }



                DataTable usrTable = new DataTable();
                DataRow newRow;

                usrTable.Columns.Add("Id");
                usrTable.Columns.Add("Name");
                usrTable.Columns.Add("Last Updated By");
                usrTable.Columns.Add("Last Updated Date");

                StructureFacade sFac = new StructureFacade();
                foreach (SalesOne.BusinessObjects.User usr in sFac.GetBlockedUsers(uName, page))
                {
                    string lastUpBy = "";
                    string updatedDate = "";
                    string name = "";

                    if (usr != null)
                    {
                        MembershipUser adUser = Membership.GetUser(usr.LastUpBy);
                        MembershipUser u = Membership.GetUser(usr.UserName);

                        lastUpBy = adUser.UserName;
                        updatedDate = usr.LastUpdateDate;
                        name = usr.Name;


                        newRow = usrTable.NewRow();
                        newRow[0] = usr.UserName;
                        newRow[1] = name;
                        newRow[2] = lastUpBy;
                        newRow[3] = updatedDate;


                        //if (u.IsApproved != false && u.IsLockedOut != true)
                        //{
                        usrTable.Rows.Add(newRow);
                        //}
                    }



                }

                if (usrTable.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                {
                    UCGrid1.IsLastPage = (sFac.GetBlockedUsers(uName, page + 1)).Count > 0 ? false : true;
                }

                int[] hideArr = { 0 };
                UCGrid1.SetDataSource(usrTable, hideArr);

                if (usrTable.Rows.Count > 0)
                {
                    ValidateUserCredentials(Request.Url.LocalPath);
                    btnUnblock.Visible = true;
                }
                else
                {
                    btnUnblock.Visible = false;
                }


            }
            catch (Exception ex)
            {

                //Response.Write(ex.Message);
            }

        }





        //private DataTable Source(string uName)
        //{
        //    StructureFacade fac = new StructureFacade();

        //    DataTable usrTable = new DataTable();
        //    DataRow newRow;

        //    usrTable.Columns.Add("Id");
        //    usrTable.Columns.Add("Name");
        //    usrTable.Columns.Add("Last Updated By");
        //    usrTable.Columns.Add("Last Updated Date");




        //    foreach (MembershipUser usr in Membership.FindUsersByName(uName))
        //    {
        //        Guid userGuuid = (Guid)usr.ProviderUserKey;
        //        string lastUpBy = "";
        //        string updatedDate = "";
        //        string name = "";
        //        BusinessObjects.User us = fac.GetUser(userGuuid);

        //        if (usr != null)
        //        {
        //            if (us.LastUpBy != Guid.Empty)
        //            {
        //                MembershipUser adUser = Membership.GetUser(us.LastUpBy);
        //                User adObj = fac.GetUser(us.LastUpBy);

        //                lastUpBy = adObj.Name;
        //                updatedDate = us.LastUpdateDate.ToShortDateString();
        //                name = us.Name;
        //            }

        //        }

        //        newRow = usrTable.NewRow();
        //        newRow[0] = usr.UserName;
        //        newRow[1] = name;
        //        newRow[2] = lastUpBy;
        //        newRow[3] = updatedDate;

        //        if (usr.IsApproved == false || usr.IsLockedOut == true)
        //        {
        //            usrTable.Rows.Add(newRow);
        //        }

        //    }
        //    if (usrTable.Rows.Count > 0)
        //    {
        //        btnUnblock.Visible = true;
        //    }
        //    else
        //    {
        //        btnUnblock.Visible = false;
        //    }

        //    return usrTable;

            
        //}

        protected void btnUnblock_Click(object sender, EventArgs e)
        {
            try
            {
                GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

                string fails = string.Empty;
                string success = string.Empty;

                foreach (GridViewRow row in gr.Rows)
                {
                    CheckBox chk = (CheckBox)row.Cells[0].FindControl("chkUsers");
                    if (chk != null && chk.Checked == true)
                    {
                        MembershipUser usr = Membership.GetUser(row.Cells[2].Text);
                        if (usr.IsApproved == false)
                        {
                            usr.IsApproved = true;
                            Membership.UpdateUser(usr);

                        }

                        if (usr.IsLockedOut == true)
                        {
                            usr.UnlockUser();
                            Membership.UpdateUser(usr);
                        }

                        MassageSummery("IIS407");

                    }
                }

                Source(Session["SUsr"].ToString(), pageNo);
            }
            catch (Exception)
            {
                MassageSummery("IIS502");
            }
            
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                //string uName = "%" + txtUserName.Text + "%";
                //int[] numbers = { 0 };
                //UCGrid1.SetDataSource(Source(uName,1), numbers);
                pageNo = 1;
                Source(string.Empty, 1);

            }
            catch (Exception ex)
            {
                MassageSummery("IIS504");
                //Response.Write(ex.Message);
            }
        }




        #region IntTest Members

        public void Next()
        {
            try
            {
                if (UCGrid1.GetText() != "1")
                {
                    pageNo = Convert.ToInt32(UCGrid1.GetText()) + 1;
                    UCGrid1.SetText(pageNo.ToString());

                }
                else
                {
                    pageNo = 2;
                    UCGrid1.SetText("2");

                }

                Source(string.Empty, pageNo);
            }
            catch (Exception)
            {

            }
            
        }

        public void Previous()
        {
            try
            {
                if (UCGrid1.GetText() != "1")
                {
                    pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                    UCGrid1.SetText(pageNo.ToString());

                    Source(string.Empty, pageNo);
                }
            }
            catch (Exception)
            {

            }
            
        }

        public void Go()
        {
            try
            {
                int p = int.Parse(UCGrid1.GetText());
                if (p != 0)
                {
                    try
                    {
                        Source(Session["SUsr"].ToString(), p);
                        pageNo = p;
                    }
                    catch (Exception)
                    {

                        throw;
                    }


                }
            }
            catch (Exception)
            {

            }
            

        }

        #endregion
    }
}
